package test.algos.sorting;

import java.util.ArrayList;
import java.util.List;

public class SortUtil {

	public static Integer safeGet(List<Integer> list, Integer index) {

		if (index < list.size()) {
			return list.get(index);
		}

		return null;
	}

	public static List<? extends Number> safeGetSublist(
			List<? extends Number> list, Integer startIndex, Integer endIndex) {

		if (startIndex != null && endIndex != null) {
			int listSize = list.size();
			if (startIndex < listSize) {

				if (endIndex <= listSize) {
					return list.subList(startIndex, endIndex);
				} else {
					return list.subList(startIndex, listSize);
				}
			}
		}
		return new ArrayList<>();
	}

	public static Integer compareAndReturn(Integer leftElem, Integer rightElem) {

		if (leftElem != null && rightElem != null) {
			return Integer.compare(leftElem, rightElem);

		}

		if (leftElem == null && rightElem != null) {
			return 1;
		}
		if (rightElem == null && leftElem != null) {
			return -1;
		}

		return null;
	}

	public static void swap(Integer leftIndex, Integer rightIndex,
			List<Integer> input) {

		Integer temp = input.get(leftIndex);

		input.set(leftIndex, input.get(rightIndex));
		input.set(rightIndex, temp);

	}

}
